/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jmediaplayer;

import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import jmediaplayer.events.*;
import org.jaudiotagger.audio.AudioFile;

/**
 * The media player
 * @author Duy Nguyen, PHUOCVIET
 */
public class JMediaPlayer
        extends JFrame
        implements ClipboardOwner {

    public static String CMD_EDITTAG = "CMDEditTag";
    public static String CMD_CPYLOCATION = "CMDCopyLocation";
    private JPlayer m_JPlayer;
    private JPlaylist m_JPlaylist;
    private JPopupMenu m_PopupMenu;

    /**
     * Constructer for JMediaPlayer with custom title
     * @param title
     * @throws HeadlessException
     */
    public JMediaPlayer(String title) throws HeadlessException {
        super(title);

        Initialize();
    }

    /**
     * Constructer for JMediaPlayer with default title "JMediaPlayer"
     * @throws HeadlessException
     */
    public JMediaPlayer() throws HeadlessException {
        super("jMediaPlayer");

        Initialize();
    }

    /**
     * Initialize components: set layouts, create and arrange components
     */
    private void Initialize() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Set layout to null
        this.setLayout(new java.awt.BorderLayout(5, 5));
        this.setMinimumSize(new Dimension(JPlayer.PLAYER_WIDTH + 10, JPlayer.HEIGHT));
        this.setBounds(0, 0, JPlayer.PLAYER_WIDTH, JPlayer.PLAYER_HEIGHT + 300);

        // initialize player
        m_JPlayer = new JPlayer();
        this.add(m_JPlayer, java.awt.BorderLayout.PAGE_START);

        m_JPlaylist = new JPlaylist();
        // m_jPlaylist.setPlayer(m_jPlayer);
        m_JPlaylist.addPlaylistListener(new PlaylistListener() {

            public void PlaylistActionPerformed(PlaylistEvent e) {
                playlistActionPerformed(e);
            }
        });

        m_JPlayer.setPlaylist(m_JPlaylist);
        this.add(m_JPlaylist, java.awt.BorderLayout.CENTER);

        initPopupMenu();
    }

    private void initPopupMenu() {
        m_PopupMenu = new JPopupMenu();

        JMenuItem popupMenuItem = new JMenuItem("Edit ID3v2Tag");
        popupMenuItem.setActionCommand(JMediaPlayer.CMD_EDITTAG);
        popupMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
                btnPopupItemActionPerformed(e);
            }
        });
        m_PopupMenu.add(popupMenuItem);

        popupMenuItem = new JMenuItem("Copy location");
        popupMenuItem.setActionCommand(JMediaPlayer.CMD_CPYLOCATION);
        popupMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(ActionEvent e) {
                btnPopupItemActionPerformed(e);
            }
        });
        m_PopupMenu.add(popupMenuItem);

        MouseListener popupListener = new PopupListener(m_JPlaylist.getSongList());
        m_JPlaylist.getSongList().addMouseListener(popupListener);
    }

    public void lostOwnership(Clipboard clipboard, Transferable contents) {
        // do nothing
    }

    /**
     * Listener class for popup menu
     * @author Duy Nguyen
     */
    class PopupListener extends MouseAdapter {

        private JList songList;

        public PopupListener(JList songList) {
            this.songList = songList;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            processPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            processPopup(e);
        }

        public void processPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                int index = songList.locationToIndex(e.getPoint());
                songList.setSelectedIndex(index);
                m_PopupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    }

    /**
     * Open Tag Editor
     * @param e
     */
    private void btnPopupItemActionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (command != null) {
            if (command.equals(JMediaPlayer.CMD_EDITTAG)) {
                if (!m_JPlaylist.getSongList().isSelectionEmpty()) {
                    Object selectedValue = m_JPlaylist.getSongList().getSelectedValue();
                    if (selectedValue instanceof AudioFile) {
                        JTagEditor tagEditor = new JTagEditor(this, (AudioFile) selectedValue);
                        tagEditor.setVisible(true);
                    }
                }
            } else if (command.equals(JMediaPlayer.CMD_CPYLOCATION)) {
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                Object selectedValue = m_JPlaylist.getSongList().getSelectedValue();
                if (selectedValue instanceof AudioFile) {
                    StringSelection stringSelection = new StringSelection(((AudioFile) selectedValue).getFile().getPath());
                    clipboard.setContents(stringSelection, (ClipboardOwner) this);
                }
            }
        }
    }

    private void playlistActionPerformed(PlaylistEvent e) {
        switch (e.getAction()) {
            case PlaylistEvent.PLAY: {
                Object item = e.getActionItem();
                if (item instanceof AudioFile) {
                    m_JPlayer.PlayAction((AudioFile) item);
                }
            }
            break;
            case PlaylistEvent.STOP: {
                m_JPlayer.StopAction();
            }
        }

    }
}
